#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""Get levels of baidu tieba

Example
------
>>> print(getlevels('妹子图'))
{'爆奶': 6, '长发': 1, '包臀': 13, '媚眼': 2, '肥臀': 8, '跪地': 12, '香肩': 5, '红唇': 4, '丁字裤': 10, '嫩舌': 3, '蛮腰': 7, '挤胸': 11, '黑丝': 9}

>>> print(getlevels('美女吧', pn=10))
{'翩若惊鸿': 9, '般般入画': 1, '沉鱼落雁': 10, '天生丽质': 5, '方桃譬李': 2, '剪水双瞳': 4, '如花似玉': 7, '百媚千娇': 6, '群芳难逐': 8, '秀外慧中': 3, '美艳绝伦': 13, '闭月羞花': 11, '国色天香': 12, '风华绝代': 14}
"""

import bs4
import requests


def _getlevels(href):
    URL = "http://tieba.baidu.com%s" % href

    response = requests.get(URL)
    soup = bs4.BeautifulSoup(response.content, "lxml")
    return {t.text:int(v.text) for t, v in zip(soup.find_all('div', {'class': 'd_badge_title'}), soup.find_all('div', {'class': 'd_badge_lv'}))}


def getlevels(kw, pn=30, e='utf-8'):
    """Get levels of baidu tieba with a keyword
    
    Arguments:
        kw {str} -- keyward
    
    Keyword Arguments:
        pn {number} -- number of pages (default: {30})
        e {str} -- encoding (default: {'utf-8'})
    
    Returns:
        dict -- level name: level value

    ------
    Call: _getlevels
    """
    URL = 'http://tieba.baidu.com/f'
    response = requests.post(URL, data={'kw': kw, 'pn':pn, 'e':e})
    soup = bs4.BeautifulSoup(response.text, "lxml")
    levels = {}
    ul = soup.find('ul', {'id': 'thread_list'})
    for li in ul.find_all('li', {'class': 'j_thread_list clearfix'}):
        a = li.find('a', {'title':True})
        href = a['href']
        # print(href)
        lst = _getlevels(href)
        levels.update(lst)
    return levels

print(getlevels('美女吧', pn=10))
